package com.yyplatform.admin.platform.log.service.impl;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.infra.utils.DateUtils;
import com.yyplatform.admin.platform.log.dto.SysErrorLogDTO;
import com.yyplatform.admin.platform.log.dto.SysErrorLogQueryDTO;
import com.yyplatform.admin.platform.log.entity.SysErrorLogEntity;
import com.yyplatform.admin.platform.log.repository.SysErrorLogRepository;
import com.yyplatform.admin.platform.log.service.SysErrorLogService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-20
 */
@Service
@AllArgsConstructor
@Slf4j
public class SysErrorLogServiceImpl implements SysErrorLogService {

    private SysErrorLogRepository sysErrorLogRepository;
    private ModelMapper modelMapper;

    @Override
    public void delete(Long id) {
        sysErrorLogRepository.deleteById(id);
    }

    @Override
    public Page<SysErrorLogDTO> query(SysErrorLogQueryDTO queryDTO) {
        Page<SysErrorLogEntity> pageImpl = sysErrorLogRepository.findPage(
                queryDTO.getUsername(),
                DateUtils.getBeginDate(queryDTO.getCreateTimeArr()),
                DateUtils.getEndDate(queryDTO.getCreateTimeArr()),
                queryDTO.getPageRequest().withSort(Sort.by("id").descending())
        );

        List<SysErrorLogDTO> lit =
                pageImpl.getContent().stream().map(e -> modelMapper.map(e, SysErrorLogDTO.class)).toList();
        return new PageImpl<>(lit, queryDTO.getPageRequest(), pageImpl.getTotalElements());
    }

    @Override
    public void deleteAll() {
        sysErrorLogRepository.deleteAll();
    }
}
